import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 
 */

/**
 * @author Lucky
 *
 */
public class FidBugidMap {

	/**
	 * @param args
	 */
	private String bugid="";
	private String svnid="";
	private String fid="";
	public int count;
	private Connection conn=null;
	
	public void getFiles()
	{
		
		count=0;
		String query1= "SELECT * FROM fid_svnid";
		//String query1= "SELECT * FROM fid_svnid limit 0,60000";
		try {
			//int i=50000;
			int i=0;
			Statement stmt=conn.createStatement();
			stmt.execute(query1);
			ResultSet res=stmt.getResultSet();
			while(res.next())
			{
				fid=res.getString("fid");
				svnid=res.getString("svnid");
				getBugid(fid,svnid);
				System.out.println("Done for "+i+" fid="+fid+" svnid="+svnid);
				i++;
			}
			if(stmt != null)
				stmt.close();
			} catch (SQLException e) {
			e.printStackTrace();
			//interrupt();
		}
			
			
		System.out.println("Count="+count);	
	}
	
	public void getBugid(String fid, String svnid)
	{
	
		String query="SELECT * FROM bug_svn_info where svn_id like \"%"+svnid+"%\"";
		System.out.println("query="+query);
		try {
			Statement stmt=conn.createStatement();
			stmt.execute(query);
			ResultSet res=stmt.getResultSet();
			while(res.next())
			{
				
				String svnlist[]=res.getString("svn_id").split(",");
				
				int i=0;
				for(i=0;i<svnlist.length;i++)
				{
					if(svnlist[i].equals(svnid))
					{
						bugid=res.getString("bugid");
						System.out.println("I am in fi");
						System.out.println("fid="+fid+" bugdid="+bugid+" svn_id="+svnid);
						populateFidBugid();
					}
					else
					{
					 
					  System.out.println("I am in else");
					  System.out.println("fid="+fid+" bugdid="+bugid+" svn_id="+svnid);
					  //interrupt();
					}
				}
				
				if(svnlist.length>=1)
				{System.out.println("fid="+fid+" svnid"+svnid+"  bugid"+bugid);
				//interrupt();
				count++;
				}
				else
				{
					System.out.println("==>"+"fid="+fid+"  svnid="+svnid+"  bugid="+bugid);
					//interrupt();
				}
			}
			if(stmt != null)
				stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
			
			
			//interrupt();
		}
		
  System.out.println("-------------------------------------\n\n");		
	}
	
	public void populateFidBugid()
	{
		String query="Insert into fid_bugid values ('"+fid+"','"+bugid+"','"+svnid+"')";
		Statement stmt;
		try {
			stmt = conn.createStatement();
			stmt.executeUpdate(query);
			
			if(stmt != null)
				stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
			//interrupt();
		}
	}
	
	/*public void interrupt()
	{
		BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
		System.out.println("INTPR");
		try {
			br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}*/
	
	public static void main(String[] args) {
		FidBugidMap fbm=new FidBugidMap();
		db_conn condb=new db_conn();
		fbm.conn=condb.connect("jboss","root","123");
		fbm.getFiles();
		
	}

}
